#include <Arduino.h>
#include <TFT_eSPI.h> // Graphics and font library for ST7735 driver chip
#include <SPI.h>
#include <WiFi.h>
#include <Adafruit_AHTX0.h>
#include "BMP.h"


#define NTP1  "ntp1.aliyun.com"
#define NTP2  "ntp2.aliyun.com"
#define NTP3  "ntp3.aliyun.com"
#define WIDTH  160
#define HEIGHT 80
#define I2C_SDA 4 //传感器I2C使用的管脚
#define I2C_SCL 5
#define Key0 8  //up
#define Key1 13 //down
#define Key2 5  //right
#define Key3 9  //left
#define Key4 4  //mid 暂时没有使用
#define REDLED 12 //WIFI开始连接，亮灯，连接成功后，灯灭

TFT_eSPI tft = TFT_eSPI(); // Invoke library, pins defined in User_Setup.h
TFT_eSprite spr = TFT_eSprite(&tft);
const char* ssid     = "Tanggy666";    	 // WIFI账户
const char* password = "1qaz!QAZ";	 // WIFI密码


Adafruit_AHTX0 aht;
WiFiClient client;

void Humidity()
{
  sensors_event_t humidity, temp;
  aht.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data

  spr.drawRect(0, 0, WIDTH, HEIGHT, TFT_WHITE);
  spr.setCursor(0, 0);

  spr.fillSprite(TFT_WHITE);
  spr.setTextColor(TFT_BLACK, TFT_WHITE);
  spr.setTextFont(2);
  spr.setTextSize(2);
  spr.pushImage(8,20,40,40,bmp6_humnew); spr.setCursor(60, 24);
  spr.print(humidity.relative_humidity,1);spr.println("%");
  spr.pushSprite(0, 0);
}


void setup() {
  Serial.begin(115200);
  pinMode(12,OUTPUT);
  pinMode(13,OUTPUT);


  tft.init();
  // 显示方向
  tft.setRotation(3);
  tft.fillScreen(TFT_BLACK);//LCD清空
  tft.setCursor(0, 0);
  tft.setTextSize(1);

  WiFi.begin(ssid, password);
  tft.print("WIFI Connecting");

  while (WiFi.status() != WL_CONNECTED)
  {
    //tft.print("WIFI Connecting");
  }
  tft.print("WIFIsuccess");

  while (!aht.begin())
  {
    Serial.println(F("AHT10 not connected or fail to load calibration coefficient")); //(F()) save string to flash & keeps dynamic memory free
    delay(100);
  }
  tft.print("aht success");





  //tft.setTextColor(TFT_YELLOW, TFT_BLACK); // Note: the new fonts do not draw the background colour
}

void loop() {
  // 延迟时间
  delay(5000);
  Serial.println("hello esp32c3");
  digitalWrite(13,!digitalRead(13));
  digitalWrite(12,!digitalRead(12));
   Humidity();

}
